#include "drawing.h"

drawing::drawing(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui_drawing)
{
    ui->setupUi(this);


    
    // 确保目录存在
    QDir dir("/home/zk/qt-learning/img");
    if(!dir.exists()) {
        dir.mkpath(".");
    }
    
    QPixmap pixmap(200, 300);
    
    pixmap.fill(Qt::gray);
    QPainter painter(&pixmap);
    painter.setPen(Qt::blue);
    painter.drawEllipse(10, 10, 100, 100);
    pixmap.save("/home/zk/qt-learning/img/pix_map.png");
    
    
    // 创建一个200x300的QBitmap
    QBitmap bitmap(200, 300);
    bitmap.fill(Qt::color1);
    
    // 创建新的QPainter对象用于绘制QBitmap
    QPainter bitmapPainter(&bitmap);
    bitmapPainter.setPen(Qt::color0);  // 使用Qt::color1而不是Qt::blue
    bitmapPainter.setBrush(Qt::color0);  // 使用Qt::color1而不是Qt::black
    bitmapPainter.drawRect(50, 50, 100, 100);
    // 转换并保存
    QPixmap pixmap_1 = QPixmap::fromImage(bitmap.toImage());
    if(!pixmap_1.save("/home/zk/qt-learning/img/bit_map.png")) {
        qDebug() << "保存失败";
    }
    
    
    
    QImage img(200, 300, QImage::Format_RGB32);
    img.fill(Qt::gray);
    
    QPainter imgPainter(&img);
    QPicture pic;
    imgPainter.begin(&pic);
    imgPainter.setPen(Qt::red);
    imgPainter.setBrush(Qt::yellow);
    imgPainter.drawRect(50, 50, 50, 100);
    imgPainter.end();
    pic.save("/home/zk/qt-learning/img/test.pic");

    img.save("/home/zk/qt-learning/img/img.png");




}

drawing::~drawing()
{
    delete ui; 
}

void drawing::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);

    //将图片显示再UI上
    painter.drawPixmap(0, 0, QPixmap("/home/zk/qt-learning/img/pix_map.png"));
    painter.drawPixmap(200, 0, QPixmap("/home/zk/qt-learning/img/bit_map.png"));
    painter.drawPixmap(400, 0, QPixmap("/home/zk/qt-learning/img/img.png"));

    QImage image(200,300,QImage::Format_RGB32);
    image.fill(Qt::lightGray);
    
    for(int i = 50; i < 150; i=i+2) {
        for(int j = 50; j < 150; j=j+2) {
            image.setPixel(i, j, qRgb(0, 0, 255));
        }
    }


    painter.drawImage(600, 0, image);


    QPicture pic;
    pic.load("/home/zk/qt-learning/img/test.pic");
    painter.drawPicture(300, 0, pic);
}

